package com.fantasy.easy.biz.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fantasy.easy.biz.domain.BizColla;
import com.fantasy.easy.biz.domain.BizCredit;
import com.fantasy.easy.biz.service.BizBaseService;
import com.fantasy.easy.biz.service.BizCollaService;
import com.fantasy.easy.biz.service.BizCreditService;
import com.fantasy.easy.core.SessionSubjectUtils;
import com.fantasy.easy.core.entity.SysUserEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @Author sdehua-帅德华
 * @Email sdhstart@qq.com
 * @Date 2022/6/24 14:47
 * @Version 1.0
 **/
@RestController
@RequestMapping("credit")
public class BizCreditController {

    @Resource
    BizCreditService bizCreditService;
    @Resource
    BizBaseService bizBaseService;

    @GetMapping("info")
    public IPage<BizCredit> getInfo(@RequestBody(required = false) BizCredit bizCredit, Page<BizCredit> p){
        SysUserEntity user = SessionSubjectUtils.getUserEntity();
        if (!user.getName().equals("admin")){
            int id = bizBaseService.getIdByName(user.getName());
            BizCredit credit = new BizCredit();
            credit.setId(id);
            return bizCreditService.queryAll(credit,p);
        }
        return bizCreditService.queryAll(bizCredit,p);
    }

    @PostMapping("add")
    public int addInfo(@RequestBody BizCredit bizCredit){
        int credit = 100 - (bizCredit.getHasdebt() + bizCredit.getIsaccused() + bizCredit.getHascrime()) * 10;
        if (credit < 0){
            credit = 0;
        }
        bizCredit.setCredit(credit);
        return bizCreditService.insert(bizCredit);
    }


}
